package com.nowar.common.util;

import com.nowar.common.constant.Constant;
import org.apache.commons.text.StringSubstitutor;
import org.springframework.util.PropertyPlaceholderHelper;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: 谢无殇。
 * @Date : 2023-01-17 21:53
 */
public class PropertyResolverHelper {

    private static PropertyPlaceholderHelper defaultResolverProperty =
            new PropertyPlaceholderHelper(Constant.DEFAULT_PLACEHOLDER_PREFIX, Constant.DEFAULT_PLACEHOLDER_SUFFIX,
                    Constant.DEFAULT_SEPARATOR, true);
    private static PropertyPlaceholderHelper defaultResolver =
            new PropertyPlaceholderHelper(Constant.DEFAULT_PLACEHOLDER_PREFIX, Constant.DEFAULT_PLACEHOLDER_SUFFIX);


    public static String getDefaultValueByProperty(String placeholder, String value) {
        return defaultResolverProperty.replacePlaceholders(placeholder, s -> value);
    }

    public static String getDefaultValueByProperties(String placeholder, Properties properties) {
        return defaultResolverProperty.replacePlaceholders(placeholder, properties);
    }

    public static String getValueByProperties(String placeholder, Properties properties) {
        return defaultResolver.replacePlaceholders(placeholder, properties);
    }

    public static String getValueByResolverProperty(String placeholder, String value) {
        return defaultResolver.replacePlaceholders(placeholder, s -> value);
    }

    public static void main(String[] args) {
        //带true取默认值
        System.out.println(getDefaultValueByProperty("${name:1}", null));
        System.out.println(getDefaultValueByProperty("${name}", null));
        System.out.println(getDefaultValueByProperty("${name:1}", "123"));
        System.out.println(getValueByResolverProperty("${name:2}", null));
        System.out.println(getValueByResolverProperty("${name}", null));
        System.out.println(getValueByResolverProperty("${name}", "456"));

        Properties properties = new Properties();
        properties.put("nameaa", "12345566");
        properties.put("namebb", "123455667");
        properties.put("namecc", "1234556678");
        System.out.println(getDefaultValueByProperties("${nameaa}", properties));
        System.out.println(getDefaultValueByProperties("${namebb}", properties));
        System.out.println(getDefaultValueByProperties("namecc", properties));
        String sql1 = "select * from tab where id=${id}";
        String sql2 = "select * from tab${no} where id=#{name}";
        Map<String, Object> map = new HashMap<>();
        map.put("id", 12);
        map.put("no", "_01");
        //$拼接
        System.out.println(resolverPropertyByMap(map, sql1));
        System.out.println(resolverPropertyByMap(map, sql2));
    }

    /**
     * 适用sql占位符替换
     *
     * @param resolver 格式${}
     * @return String
     */
    public static String resolverPropertyByMap(Map<String, Object> map, String resolver) {
        StringSubstitutor stringSubstitutor = new StringSubstitutor(map);
        //有默认值
        stringSubstitutor.setVariablePrefix(Constant.DEFAULT_PLACEHOLDER_PREFIX);
        stringSubstitutor.setVariableSuffix(Constant.DEFAULT_PLACEHOLDER_SUFFIX);
        return stringSubstitutor.replace(resolver);
    }
}
